package com.zhoug.speech;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

/**
 * @Author 35574
 * @Date 2020/7/8
 * @Description
 */
public class RoundBitmapUtils {

    /**
     * 创建圆形图片
     *
     * @param source
     * @return
     */
    public static Bitmap createCircleBitmap(Bitmap source) {
        int length = Math.min(source.getWidth(), source.getHeight());
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        canvas.drawCircle(length >> 1, length >> 1, length >> 1, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(source, 0, 0, paint);
        return target;
    }


    /**
     * 把bitmap剪切为圆形图片
     *
     * @param bitmap 原图
     * @param ratio  剪切原图大小的百分比 默认：1
     * @param config {@link Bitmap.Config} 默认值{@link Bitmap.Config#ARGB_4444}
     * @return
     */
    public static Bitmap toRoundBitmap(Bitmap bitmap, float ratio, Bitmap.Config config) {
        if (bitmap == null) {
            return null;
        }
        if (config == null) {
            config = Bitmap.Config.ARGB_4444;
        }
        if (ratio <= 0 || ratio > 1) {
            ratio = 1;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        //根据比例在图片中间取正方形
        //正方形的边长
        int size = (int) (Math.min(width, height) * ratio);
        //图片中取得正方形的坐标
        Rect src = new Rect();
        src.left = (width - size) / 2;
        src.top = (height - size) / 2;
        src.right = src.left + size;
        src.bottom = src.top + size;
        //新的画布的区域
        Rect dst = new Rect(0, 0, size, size);

        //定义输出图片
        Bitmap outBitmap = Bitmap.createBitmap(size, size, config);
        Canvas canvas = new Canvas(outBitmap);
        //背景透明
        canvas.drawColor(Color.TRANSPARENT);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        //中心画圆
        canvas.drawCircle(size >> 1, size >> 1, size >> 1, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint);
        return outBitmap;
    }

    /**
     * 把bitmap剪切为圆形图片
     *
     * @param bitmap
     * @return
     */
    public static Bitmap toRoundBitmap(Bitmap bitmap) {
        return toRoundBitmap(bitmap, 1, null);
    }

    /**
     * 生成圆角图片
     * @param bitmap  原图
     * @param corners 角度
     * @param lt      左上角
     * @param lb      左下角
     * @param rt      右上角
     * @param rb      右下角
     * @return
     */
    public static Bitmap toCornersBitmap(Bitmap bitmap, int corners, boolean lt, boolean rt, boolean rb, boolean lb) {
        if (bitmap == null) {
            return null;
        }
        if (corners <= 0 || !(lt || lb || rt || rb)) {
            return bitmap;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //最大角度
        int maxCorners = Math.min(width, height) / 2;
        if (corners > maxCorners) {
            corners = maxCorners;
        }
        Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(outBitmap);
        Path path = new Path();
        //左上角
        path.moveTo(0, corners);
        if (lt) {
            path.quadTo(0, 0, corners, 0);

        } else {
            path.lineTo(0, 0);
        }
        path.lineTo(width - corners, 0);
        //右上角
        if (rt) {
            path.quadTo(width, 0, width, corners);
        } else {
            path.lineTo(width, 0);
        }
        path.lineTo(width, height - corners);
        //右下角
        if (rb) {
            path.quadTo(width, height, width - corners, height);
        } else {
            path.lineTo(width, height);
        }
        path.lineTo(corners, height);
        //左下角
        if (lb) {
            path.quadTo(0, height, 0, height - corners);
        } else {
            path.lineTo(0, height);
        }
        path.lineTo(0, corners);
        canvas.clipPath(path);
        canvas.drawBitmap(bitmap, 0, 0, null);
        return outBitmap;
    }

    /**
     * 生成圆角图片,4个角
     *
     * @param bitmap  原图
     * @param corners 角度
     * @return
     */
    public static Bitmap toCornersBitmap(Bitmap bitmap, int corners) {
        return toCornersBitmap(bitmap, corners, true, true, true, true);
    }
}
